{% extends 'frame.html' %}

{% block right-container %}
    <div class="right-container">
    <div class="container" style="width:100%;float:left">
        <ul class="list-group filelist">
        {% if ProjectList %}
            <li class="list-group-item list-group-item-info ">
                <span>
                    <a href="" class="btn">返回上级···</a>
                </span>
            </li>
        {% endif %}
        </ul>
    </div>
    <p class="result" style="background-color: #5bc0de">{{ ProjectList }}</p>
    <p class="result2" style="background-color: #c8e5bc"></p>
    </div>
{% endblock %}


{% block customjs %}
    <script type="text/javascript">
        $(document).ready(function () {
            var CurPath = "";
            function LoadList(Data){
                if(CurPath == ''){
                }
                else{
                    $(".filelist").append("<li class='list-group-item list-group-item-success updirectory fileLi'><span class='uppath'>返回上级</span></li>");
                }
                for(m in Data){
                    if (Data[m].mode == '040000'){
                        $(".filelist").append("<li class=\"list-group-item list-group-item-success directory fileLi\">" +
                            "<span><a class=\"expanding btn\">"+Data[m].name+"</a></span>" +
                            "<span class=\"adddirs\" style=\"float:right;cursor: pointer\">" +
                            "添加" +
                            "</span>" +
                            "</li>")
                    }
                    else if(Data[m].mode == '100644') {
                        $(".filelist").append('<li class="list-group-item list-group-item-danger file fileLi">'+Data[m].name+'</li>')
                    }
                }
                $(".expanding").click(function () {
                    console.log($(this).text());
                    FilePath = $(this).text();
                    if (CurPath == "") {
                        CurPath += FilePath;
                    }
                    else{
                        CurPath += '/'+FilePath;
                    }
                    $.ajax({
                        url: '/getfilelist/',
                        dataType: 'json',
                        type: 'GET',
                        data: {
                            FilePath: CurPath
                        },
                        success: function (result) {
                            $(".fileLi").remove();
                            LoadList(result);
                        },
                        error: function (result) {
                            console.log(result);
                        }
                    });

                });
                function UpPath(path) {
                    $.ajax({
                            url: '/getfilelist/',
                            dataType: 'json',
                            data: { 'FilePath': path },
                            type: 'get',
                            success: function (result) {
                                $(".fileLi").remove();
                                CurPath = path;
                                LoadList(result);
                                console.log(CurPath);
                            },
                            error: function (result) {
                                alert(result);
                            }
                        });
                }
                $(".uppath").click(function(){
                    var UpPath1 = CurPath.split('/');
                    console.log(UpPath1.length);
                    if (UpPath1.length == 2){
                        console.log("Up path is 1<n<2");
                        var UpPath2 = UpPath1[0];
                        UpPath(UpPath2);
                    }
                    else if (UpPath1.length == 1){
                        var UpPath2 = '';
                        console.log("Up path is 1");
                        UpPath(UpPath2);
                    }
                    else{
                        for (i=0;i<UpPath1.length-2;i++){
                            console.log("Up path is more then 3");
                            var UpPath2 = UpPath1[i]+'/'+UpPath1[i+1];
                            UpPath(UpPath2);
                        }
                    }

                });
            }
            $.ajax({
                url: '/getfilelist/',
                dataType: 'json',
                type: 'GET',
                data: {
                    FilePath: CurPath
                },
                success: function (result){
                    LoadList(result);
                },
                error: function (result) {
                    console.log(result);
                }
            });
        });
    </script>
{% endblock %}